<?php
declare (strict_types = 1);
/**
 * 管理员
 */
namespace app\common\admin;

use app\common\facade\model\MaterialModel; // 引入素材类模型门面代理
use app\common\facade\model\MatTypeModel; // 引入素材类型门面代理

class Material extends BaseAdmin
{
    /**
     * 素材列表
     */
    public function matList($param = [])
    {
        $where = [];

        $ordername = 'mat_id';
        $order = 'desc';
        
        if(isset($param['ordername']) || isset($param['order'])) {
            $ordername = $param['ordername'] ?: 'mat_id';
            $order = $param['order'] == 'descending' ? 'desc' : 'asc';
        }

        // 类型树形检索
        if(isset($param['type']) && count($param['type']) == 2 && $param['type'][1] != '' ) {
            if($param['type'][0] == 'mat_type') {
                $where[] = [$param['type'][0], 'in', $param['type'][1] ];
            } else {
                $where[] = [$param['type'][0], 'find in set', $param['type'][1]];
            }                        
        }
        // var_dump($where);
        // var_dump($where);        
        
        if (isset($param['query']) && !empty($param['query'])) $where[] = ['mat_name', 'like', "%{$param['query']}%"];

        $matList = MaterialModel::matList($where, [], $param['offset'], $param['length'], $ordername, $order);

        return $matList;
    }

    public function matDialogList($param = [])
    {
        $where = [];

        $ordername = 'mat_id';
        $order = 'desc';

        if(isset($param['sort']) && !empty($param['sort'])) {
            $sortArr = explode(',', $param['sort']);
            $ordername = $sortArr[0];
            $order = $sortArr[1];            
        }

        // 类型树形检索
        if(isset($param['type']) && !empty($param['type']) ) {
            $where[] = ['mat_type', 'in', $param['type']];                      
        }
        // var_dump($where);
        // var_dump($where);        
        
        if (isset($param['name']) && !empty($param['name'])) $where[] = ['mat_name', 'like', "%{$param['name']}%"];

        $matList = MaterialModel::matList($where, [], $param['offset'], $param['length'], $ordername, $order);

        return $matList;
    }

    /**
     * 素材 新增，编辑
     */
    public function matAddEditSave($param)
    {
        $where = [];
        if(isset($param['mat_id'])) {
            $where[] = ['mat_id', '=', $param['mat_id']];
            unset($param['mat_id']);
        }        
        $param['use_channel'] = is_array($param['use_channel']) ? implode(",", $param['use_channel']) : '';
        $param['s_date'] = $param['date_limit'] == 1 ? date('Y-m-d', strtotime($param['s_date'])) : '';
        $param['e_date'] = $param['date_limit'] == 1 ? date('Y-m-d', strtotime($param['e_date'])) : '';        
        // var_dump($param);
        MaterialModel::addEditSave($param, $where);
        return true;
    }

    /**
     * 素材删除
     */
    public function matDelete($param = [])
    {
        
        if (isset($param['mat_id']) && !empty($param['mat_id'])) $where[] = ['mat_id', 'in', $param['mat_id']];

        if (!isset($where)) exception();
        
        return MaterialModel::deleteData($where);
    }

    /* 获取素材类型列表  */
    public function mattypeList($param) {
        $where[] = ['mat_type_id', '>', 0];
        $field   = ['mat_type_id, mat_type_name, parent_type, oper_rights, remark'];
        $list = MatTypeModel::listsAll($where, $field, 'mat_type_id asc')->toArray();

        if(empty($list)) exception('节目类型为空');  
        if(isset($param['id']) && $param['id'] > 0) {
            $root = $param['id'];
        } else {
            $root = 0;
        }
          
        $data     = MatTypeModel::MatTypeTree($list, $root);
        return $data;
    }

    public function mattypeAddEditSave($param)
    {
        $where = [];
        if(isset($param['mat_type_id'])) {
            $where[] = ['mat_type_id', '=', $param['mat_type_id']];
            unset($param['mat_type_id']);
        }
        
        if(isset($param['oper_rights']) && !empty($param['oper_rights'])) {
            $param['oper_rights'] = implode(',' , $param['oper_rights']);
        }

        return MatTypeModel::addEditSave($param, $where);
    }

    public function mattypeDel($ids) {
        // 当ids 不为空时
        if(!empty($ids)) {
            $res = MatTypeModel::deleteData([['mat_type_id', 'in', $ids]]);
        } else {
            return [
                'code' => 0,
                'msg'  => '参数未定义'
            ];
        }

        return [
            'code' => $res ? 1 : 0,
            'msg'  => $res ? '删除成功' : '删除失败'
        ];
    }
}